<template>
  <view class="content">
    <NavBar></NavBar>
    <scroll-view
        :refresher-enabled="true"
        @refresherrefresh="handleRefresh"
        @scrolltolower="getMore"
        :refresher-triggered="isTriggered"
        scroll-y
        class="main"
    >
      <!-- 广告区域 -->
      <XtxCarousel :bannerList="bannerList"/>
      <!-- 前台类目 -->
      <CateScroll :category="category"/>
      <!-- 人气推荐 -->
      <HotPanel :hotData="hotData"/>

      <!-- 新鲜好物 -->
      <!-- 新鲜好物 -->
      <view class="panel fresh">
        <view class="title">
          新鲜好物
          <navigator
              hover-class="none"
              class="more"
              url="/pages/recommend/index?type=5"
          >
            更多
          </navigator>
        </view>
        <scroll-view scroll-x style="width: 100vw">
          <view class="cards">
            <navigator
                hover-class="none"
                :url="`/pages/goods/goods?id=${item.id}`"
                v-for="item in homeNew"
                :key="item.id"
            >
              <image mode="aspectFit" :src="item.picture"></image>
              <view class="name">{{ item.name }}</view>
              <view class="price">
                <text class="small">¥</text>
                {{ item.price }}
              </view>
            </navigator>
          </view>
        </scroll-view>
      </view>
      <!-- 热门品牌 -->
      <view class="panel brands">
        <view class="title">
          热门品牌
          <navigator hover-class="none" class="more" url="/pages/list/index">
            更多
          </navigator>
        </view>
        <view class="cards">
          <navigator
              hover-class="none"
              :url="`/pages/goods/goods?id=${item.id}`"
          >
            <image
                mode="aspectFit"
                src="http://static.botue.com/erabbit/static/uploads/brand_logo_1.jpg"
            ></image>
            <view class="name">小米</view>
            <view class="price">99元起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
                mode="aspectFit"
                src="http://static.botue.com/erabbit/static/uploads/brand_logo_2.jpg"
            ></image>
            <view class="name">TCL</view>
            <view class="price">199起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
                mode="aspectFit"
                src="http://static.botue.com/erabbit/static/uploads/brand_logo_3.jpg"
            ></image>
            <view class="name">饭小宝</view>
            <view class="price">9.9起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
                mode="aspectFit"
                src="http://static.botue.com/erabbit/static/uploads/brand_logo_4.jpg"
            ></image>
            <view class="name">鳄鱼</view>
            <view class="price">299起</view>
          </navigator>
        </view>
      </view>
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef"/>
    </scroll-view>
  </view>
</template>
<script>
import {
  getHomeBanner,
  getHomeCateScroll,
  getHomeNew,
  getHotApi,
} from '@/services/home'
import CateScroll from './components/CateScroll.vue'
import HotPanel from './components/HotPannel.vue'
import NavBar from './components/NavBar.vue'

export default {
  components: {
    NavBar,
    CateScroll,
    HotPanel,
  },
  data() {
    return {
      bannerList: [],
      category: [],
      hotData: [],
      homeNew: [],
      isTriggered: false, // 下拉刷新的标识，默认为false
    }
  },
  onLoad() {
    // 获取首页轮播图图片
    this.getBannerList()
    // 调用获取分类数据的接口
    this.getCategory()
    // 调用获取人们数据方法
    this.getHotData()
    // 新鲜好物
    this.getHomeNew()
  },
  methods: {
    async handleRefresh() {
      this.isTriggered = true
      // 重置页面的所有数据
      // 请求之前开启下拉刷新
      // 请求完毕之后关闭下拉刷新
      // 获取首页轮播图图片
      // 使用 promise.all
      // await: 修饰一个promise，等到这个promise成功之后，代码会继续执行【promise失败或者没有拿到结果，代码不会继续执行】
      // 如果await修饰的不是一个promise，代码如何执行？
      // 能够卡主函数执行的前提
      // 1、使用await修饰
      // 2、修饰的是一个promise
      // 后面函数内部的内容也想卡主，也必须遵循这个前提
      await Promise.all([
        this.$refs.guessRef.resetData(),
        this.getBannerList(),
        // 调用获取分类数据的接口
        this.getCategory(),
        // 调用获取人们数据方法
        this.getHotData(),
        // 新鲜好物
        this.getHomeNew(),
      ])
      this.isTriggered = false
    },
    getMore() {
      // 父组件通过ref调用子组件获取更多数据的方法
      this.$refs.guessRef.getGuessList()
    },
    async getBannerList() {
      const res = await getHomeBanner()
      this.bannerList = res.result
    },
    async getCategory() {
      const res = await getHomeCateScroll()
      this.category = res.result
    },
    async getHotData() {
      const res = await getHotApi()
      this.hotData = res.result
    },
    async getHomeNew() {
      const {result} = await getHomeNew()
      this.homeNew = result
    },
  },
}
</script>
<style lang="scss" scoped>
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}

/* 公共面板 */
.panel {
  margin: 20 rpx 20 rpx 0;
  border-radius: 10 rpx;
  background-color: #fff;

  .title {
    display: flex;
    align-items: center;
    padding: 24 rpx 24 rpx 0;
    font-size: 32 rpx;
    color: #262626;
    position: relative;

    text {
      font-size: 24 rpx;
      color: #7f7f7f;
      margin-left: 18 rpx;
    }
  }

  .more {
    position: absolute;
    right: 24 rpx;
    font-size: 26 rpx;
    color: #7f7f7f;
  }

  .cards {
    display: flex;
  }

  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;

  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254 rpx;
    border-right: 1 rpx solid #eee;
    border-top: 1 rpx solid #eee;

    &:nth-child(2n) {
      border-right: 0 none;
    }

    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }

  .cards {
    flex: 1;
    padding: 15 rpx 20 rpx;
    justify-content: space-between;

    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320 rpx;

  .price {
    line-height: 1;
    text-align: left;
    font-size: 26 rpx;
    color: #cf4444;
  }

  .small {
    font-size: 80%;
  }
}

.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15 rpx 20 rpx 20 rpx;
  line-height: 1;
  text-align: center;
}

.fresh image,
.brands image {
  width: 126 rpx;
  height: 126 rpx;
}

.fresh .name,
.brands .name {
  margin: 20 rpx 0 16 rpx;
  font-size: 24 rpx;
  color: #262626;
}

.brands {
  .price {
    font-size: 24 rpx;
    color: #999;
  }
}
</style>
